﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CMSLibrary.TagsCreate.CustomFunction.Common
{
    /// <summary>
    /// 星期
    /// 1.  Week()  今天星期几()    默认中文 
    /// 2.  Week(C)  今天星期几(语言)   指定格式化 (C:中文 | E:英文)
    /// 3.  Week(2007-12-12)    指定日期
    /// 4.  Week(2007-12-12,C)  指定日期，指定语言
    /// </summary>
    public class Custom_Week : PFunctionBase
    {
        #region 参数成员
        /// <summary>
        /// 源日期
        /// </summary>        
        DateTime source = DateTime.Now;
        /// <summary>
        /// 格式化字符串
        /// </summary>        
        string format = "C";
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少 
            /********** 4 **********/
            try
            {
                source = Convert.ToDateTime(paras[0]);
                format = Convert.ToString(paras[1]);
                FunctionBurden = BurdenIndex.four;
                return;
            }
            catch { }
            /********** 3 **********/
            try
            {
                source = Convert.ToDateTime(paras[0]);
                FunctionBurden = BurdenIndex.three;
                return;
            }
            catch { }
            /********** 2 **********/
            try
            {
                format = Convert.ToString(paras[0]);
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }
            /********** 1 **********/
            try
            {
                FunctionBurden = BurdenIndex.one;
                return;
            }
            catch { }
        }

        public override string ToString()
        {
            string result = string.Empty;
            DayOfWeek dow = source.DayOfWeek;
            switch(format.ToUpper())
            {
                case "C":
                    result = GetChinaFormat(dow);
                    break;
                case "E":
                    result = dow.ToString();
                    break;
            }            

            return result;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 返回中文格式化
        /// </summary>
        /// <param name="weeknum">枚举</param>
        /// <returns></returns>
        protected string GetChinaFormat(DayOfWeek weeknum)
        {
            string result = string.Empty;
            switch (weeknum)
            {
                case DayOfWeek.Sunday:
                    result = "星期日";
                    break;
                case DayOfWeek.Monday:
                    result = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    result = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    result = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    result = "星期四";
                    break;
                case DayOfWeek.Friday:
                    result = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    result = "星期六";
                    break;
            }
            return result;
        }
        #endregion
    }
}
